# cython: language_level=3
from collections import defaultdict
import json
import logging
import time
from xml.dom import NotFoundErr
import numpy as np
import pandas as pd
from sanic.views import HTTPMethodView
from utils.decorators import param_check
from utils.response import general_json_res
from xingwei.xw_whitelist_score_pro import get_new_score
from xingwei.xw_whitelist_score_pro_v2 import get_new_score_v2
from sanic.request import Request
from xingwei.old_v1.old_v1_service import get_ph_reg,get_old_v1_score
from xingwei.old_v1.genfang_new_v1 import cal_genfang_new_v1

class GetScore(HTTPMethodView):
    """restful接口实例"""
    
    async def get(self, request: Request, pk=0):
        return general_json_res(404, "no get method.")

    async def post(self, request: Request, pk=0):
        order_id = request.json.get("ApplyNO")
        apply_time = request.json.get("ApplyDate")
        app_data = request.json.get("AppList")
        sms_json = request.json.get("SmsList")
        y = request.json.get("Birthday")
        x = request.json.get("ApplyDate")
        marriage = request.json.get("Maritalstatus")
        sex = request.json.get("Sex")
        
        Education = request.json.get("Education")
        
        params = {
            'order_id': order_id,
            'apply_time': apply_time,
            'app_data': app_data,
            'sms_json': sms_json,
            'y': y,
            'x': x,
            'marriage': marriage,
            'sex': sex,
            'Education': Education
        }
        
        
        result = {}
        free_features = {}
        free_features_v2 = {}
        free_rule = {}
        status = 200
        message = 'success'
        try:
            free_features = get_new_score(params, request)
            free_features_v2 = get_new_score_v2(params, request)
            free_rule['XWWhitListScoreV1'] = free_features['XWWhitListScoreV1']
            free_rule['XWWhitListScoreV2'] = free_features_v2['XWWhitListScoreV2']
        except Exception as e:
            status = 500
            message = str(e)
            
        result["XWWhitListModelV1"] = free_features
        result["XWWhitListModelV2"] = free_features_v2
        result["free_rule"] = free_rule
        
        return general_json_res(status = status, msg = message, result=result)


class GetOldV1(HTTPMethodView):
    """restful接口实例"""
        
    async def get(self, request: Request, pk=0):
        # return general_json_res(404, "no get method.")
        ph_reg = request.app.state.jsons.get('ph_reg_20240715.json', None)
        return general_json_res(status = 200, msg = 'success', result=ph_reg)
    
    async def post(self, request: Request, pk=0):
        ProjectId = request.json.get("ProjectId")
        order_id = request.json.get("ApplyNO")
        apply_time = request.json.get("ApplyDate")
        app_data = request.json.get("AppList")
        sms_json = request.json.get("SmsList")
        birthday = request.json.get("Birthday")
        degree = request.json.get("Education")
        id_number = request.json.get("Phone")

        # 增加婚姻信息字段
        Maritalstatus = request.json.get("Maritalstatus")

        # 还款表
        id_number_json = request.json.get("id_number_json")
        
        params = {
            'ProjectId': ProjectId,
            'order_id': order_id,
            'apply_time': apply_time,
            'birthday': birthday,
            'degree': degree,
            'Maritalstatus': Maritalstatus,
            'app_data': app_data,
            'sms_json': sms_json,
            'id_number': id_number,
            'id_number_json': id_number_json,
        }
        
        result = {}
        free_features = {}
        free_rule = {}
        status = 200
        message = 'success'
        res = get_old_v1_score(params, request)
        free_rule['XWOldScoreV1'] = res['XWOldScoreV1']
        free_rule['id_number_json'] = id_number_json
        # try:
        #     free_features = get_old_v1_score(params, request)
        #     free_rule['XWOldScoreV1'] = free_features['XWOldScoreV1']
        # except Exception as e:
        #     status = 500
        #     message = str(e)
            
        result["free_features"] = res['free_features']
        result["free_rule"] = free_rule

        # 20240823部署跟放新客V1版本模型
        genfang_new_result = cal_genfang_new_v1(params)
        result.update(genfang_new_result)
        
        return general_json_res(status = status, msg = message, result=result)